{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出9*9乘法口诀表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i + 1):\n",
    "        print(f\"{j}*{i}={i * j}\", end='\\t')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个任意正整数，输出分解质因数。例如：输入90,打印出90=2*3*3*5。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18=2*3*3\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "def prime_factors(n):\n",
    "    factors = []\n",
    "    # 处理偶数\n",
    "    while n % 2 == 0:\n",
    "        factors.append(2)\n",
    "        n = n / 2\n",
    "    # 处理奇数\n",
    "    for i in range(3, int(math.sqrt(n)) + 1, 2):\n",
    "        while n % i == 0:\n",
    "            factors.append(i)\n",
    "            n = n / i\n",
    "    # 如果n大于2，说明还有一个大于sqrt(n)的质因数\n",
    "    if n > 2:\n",
    "        factors.append(int(n))\n",
    "    return factors\n",
    "\n",
    "num = int(input(\"请输入一个正整数：\"))\n",
    "result = prime_factors(num)\n",
    "print(f\"{num}=\", end='')\n",
    "print('*'.join(map(str, result)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该数字是5位数。\n",
      "54321\n"
     ]
    }
   ],
   "source": [
    "num_str = input(\"请输入一个不多于6位的正整数：\")\n",
    "print(f\"该数字是{len(num_str)}位数。\")\n",
    "print(num_str[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果为: 369\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入相加的数字个数：\"))\n",
    "a = int(input(\"请输入数字a：\"))\n",
    "sum_value = 0\n",
    "temp = 0\n",
    "for i in range(1, n + 1):\n",
    "    temp = temp * 10 + a\n",
    "    sum_value += temp\n",
    "print(\"结果为:\", sum_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "前5项之和为：8.392\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入项数N：\"))\n",
    "sum_value = 0\n",
    "a, b = 2, 1\n",
    "for _ in range(1, n + 1):\n",
    "    sum_value += a / b\n",
    "    temp = a\n",
    "    a = a + b\n",
    "    b = temp\n",
    "print(\"前{}项之和为：{}\".format(n, round(sum_value, 3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21\n",
      "261\n",
      "1581\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "for integer in range(1, 100000):\n",
    "    numItem_1 = math.sqrt(integer + 100)\n",
    "    numItem_2 = math.sqrt(integer + 268)\n",
    "    if numItem_1 == int(numItem_1) and numItem_2 == int(numItem_2):\n",
    "        print(integer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1个月的兔子对数: 1\n",
      "第2个月的兔子对数: 1\n",
      "第3个月的兔子对数: 2\n",
      "第4个月的兔子对数: 3\n",
      "第5个月的兔子对数: 5\n",
      "第6个月的兔子对数: 8\n",
      "第7个月的兔子对数: 13\n",
      "第8个月的兔子对数: 21\n",
      "第9个月的兔子对数: 34\n",
      "第10个月的兔子对数: 55\n"
     ]
    }
   ],
   "source": [
    "month = int(input(\"请输入月数：\"))\n",
    "a, b = 1, 1\n",
    "for _ in range(1, month + 1):\n",
    "    print(f\"第{_}个月的兔子对数: {a}\")\n",
    "    a, b = b, a + b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "任意输入一个3x3矩阵，求矩阵对角线元素之和。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵对角线元素之和为: 15\n"
     ]
    }
   ],
   "source": [
    "# 定义3x3矩阵\n",
    "matrix = [\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "    [7,8,9]\n",
    "]\n",
    " \n",
    "# 初始化对角线元素之和\n",
    "diagonal_sum = 0\n",
    " \n",
    "# 计算对角线元素之和\n",
    "for i in range(3):  # 3x3矩阵，所以只需要循环3次\n",
    "    diagonal_sum += matrix[i][i]  # 加上主对角线上的元素\n",
    " \n",
    "# 输出结果\n",
    "print(\"矩阵对角线元素之和为:\", diagonal_sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 222, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "def rotate_list(lst, m):\n",
    "    return lst[-m:] + lst[:-m]\n",
    "\n",
    "n = int(input(\"请输入整数的个数：\"))\n",
    "m = int(input(\"请输入移动的位置数：\"))\n",
    "lst = [int(input(f\"请输入第{i + 1}个整数：\")) for i in range(n)]\n",
    "print(rotate_list(lst, m))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n"
     ]
    }
   ],
   "source": [
    "def even_sum(n):\n",
    "    result = 0\n",
    "    for i in range(2, n + 1, 2):\n",
    "        result += 1 / i\n",
    "    return result\n",
    "\n",
    "def odd_sum(n):\n",
    "    result = 0\n",
    "    for i in range(1, n + 1, 2):\n",
    "        result += 1 / i\n",
    "    return result\n",
    "\n",
    "def call_function(fp, n):\n",
    "    return fp(n)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    n = int(input(\"请输入一个整数：\"))\n",
    "    if n % 2 == 0:\n",
    "        print(call_function(even_sum, n))\n",
    "    else:\n",
    "        print(call_function(odd_sum, n))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
